package org.skyui.views;

import org.skyui.controllers.LoginController;
import org.skyui.core.ChilliParametrsHolder;
import org.skyui.core.UIApplication;


import com.baycrypt.vaadinui.VaadinUIApplication;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class LoginWindow extends Window {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9030517385413114687L;
	LoginController loginController;
	LoginForm loginForm = new LoginFormImpl();

	Panel loginPanel;

	public LoginWindow(String name, LoginController controller,
			boolean isCaptive) {
		super(name);
		this.loginController = controller;
		setName(name);

		HorizontalLayout hl = new HorizontalLayout();
		hl.setMargin(true);
		Label l = new Label("");
		Label l2 = new Label("");
		l2.setHeight("10px");
		
		Label l3 = new Label(controller.getSignInMessage());
		Label l4 = new Label("");
		l4.setHeight("10px");

		// addComponent(l);

		l.setIcon(new ThemeResource("../skycommand/icons/login.png"));

		this.loginPanel = new Panel("");
		this.loginForm.setWidth("100%");
		this.loginForm.setHeight("130px");

		addComponent(this.loginPanel);
		this.loginPanel.addComponent(l);
		this.loginPanel.addComponent(l2);
		this.loginPanel.addComponent(l3);
		this.loginPanel.addComponent(l4);
		// this.loginPanel.addComponent(l2);
		if (!isCaptive)
			this.loginPanel.addComponent(this.loginForm);

		Label lfb = new Label("Facebook Login");

		this.loginPanel.addComponent(lfb);
		Button facebookLoginButton = new Button("", this.loginController
				.getFacebookButtonListener(UIApplication.getInstance()
						.getClientId(), UIApplication.getInstance()
						.getRedirectURI()));
		facebookLoginButton.setStyle(Button.STYLE_LINK);
		facebookLoginButton.setIcon(new ThemeResource(
				"../sky/images/login-button.png"));
		// organizationRegisterButton.setEnabled(false);
		if (!isCaptive
				&& !"3.0"
						.equals(VaadinUIApplication.getInstance().getVersion()))
			this.loginPanel.addComponent(facebookLoginButton);
		
		
		

		if (isCaptive) {
			Label lcova = new Label("Free WiFi Login");

			this.loginPanel.addComponent(lcova);
			Button coovaFacebookLoginButton = new Button("",
					this.loginController.getFacebookButtonListener(
							UIApplication.getInstance().getClientId(),
							UIApplication.getInstance().getRedirectURI()+"captive%2F"));// + "captive/"));

			coovaFacebookLoginButton.setStyle(Button.STYLE_LINK);
			coovaFacebookLoginButton.setIcon(new ThemeResource(
					"../sky/images/login-button.png"));
			
			
			getWindow().executeJavaScript("var time=15; function timer(){if(time>=0) setTimeout(\"document.getElementById('timer').innerHTML=time; time--; timer();\", 1000); else document.location.href='"+ChilliParametrsHolder.generateChilliFreeLoginUrl()+"'; }");
			this.addComponent(new Label("<h2>(new)If you haven't got FB account, you will be granted Internet access after <div id=\"timer\"></div> seconds...</h2>", Label.CONTENT_XHTML));
			getWindow().executeJavaScript("timer();");
			
			// organizationRegisterButton.setEnabled(false);
			if (!"3.0".equals(VaadinUIApplication.getInstance().getVersion()))
				this.loginPanel.addComponent(coovaFacebookLoginButton);
			
			this.addListener( new CloseListener()
			{ 
	            @Override
	            public void windowClose(CloseEvent e)
	            {
	            	getWindow().executeJavaScript("document.getElementByTagName('meta')[0].content='no-cache'; document.getElementByTagName('meta')[0].http-equiv='Cache-control'; document.getElementByTagName('meta')[0].httpEquiv='Cache-control'; ");
	            	getWindow().executeJavaScript("var time=15; function timer(){if(time>=0) setTimeout(\"document.getElementById('timer').innerHTML=time; time--; timer();\", 1000); else document.location.href='"+ChilliParametrsHolder.generateChilliFreeLoginUrl()+"'; }");
	    			
	    			getWindow().executeJavaScript("timer();");
	             
	            }
			});
			
		} else {

			Label l5 = new Label("");

			l5.setHeight("30px");
			this.loginPanel.addComponent(l5);

			Label l6 = new Label("Don't have a SkyTeamz Account?");
			this.loginPanel.addComponent(l6);
			Button organizationRegisterButton = new Button(
					"Create an account now", this.loginController
							.getOrganizationRegistrationListener());
			organizationRegisterButton.setStyleName(Button.STYLE_LINK);
			// organizationRegisterButton.setEnabled(false);
			this.loginPanel.addComponent(organizationRegisterButton);

			Label l7 = new Label("");

			l7.setHeight("150px");
			this.loginPanel.addComponent(l7);

			this.loginForm.addListener(this.loginController.getListener());
			
		}
	}

}